import {observable, action} from 'mobx';
import { ListStore } from './listStore'
import { WeaTools } from 'ecCom';
import * as API_LIST from '../apis/fnaExpenseCrm'
import * as PUBLIC_API from '../apis/fnaReportBase'
import { WeaLocaleProvider } from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;

/**
 * 客户收支的Store
 */
export class FnaExpenseCrmStore extends ListStore{

	//tab默认值
	@observable searchParams = {
        index:0
    };

	constructor(){
		super();
		this.title = "";
		this.initDatas = this.initDatas.bind(this);
		this.doSearch = this.doSearch.bind(this);
	}
	
	initDatas(params = {}){
		//初始化tab列表
		PUBLIC_API.queryYearsPeriods().then(data =>{
			if(data.flag == 'success'){
			    this.hasRight = 1;

				const { yearList } = data;
				let tabTitle=[];
				let size = 0;
				for(let obj of yearList){
					let title = {title:obj.year,key:obj.id,index:obj.index};
					tabTitle.push(title);
					if(obj.current){
						this.searchParams.index = obj.index;
						this.doSearch({fnayear:obj.year});
					}
					size++;
				}
				this.topTab = tabTitle;
				this.topTabCount = size;
			} else {
				this.hasRight = 4;
			}
		});
	}

	//TABLE数据
	doSearch(params = {}) {
		const newParams = {...this.searchParams,...params };

		this.loading = true; 
		API_LIST.queryData(newParams).then(data =>{
			if(data.flag == 'success'){
				this.hasRight = 1;
				this.tableStore.getDatas(data.datas,1);
	            this.dataKey = data.datas;
            } else {
                this.hasRight = 4;
            }           
		});
		this.loading = false;  
	}
}